package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.LocalVideoEpisode;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;

import java.util.ArrayList;

public class PlayerLocalAdapter extends BaseAdapter {

	private Context mContext;

	private ArrayList<LocalVideoEpisode> mLocalPlayLists;

	private LayoutInflater mInflater;

	private String mType;
	
	private String mVideoName;

	private int mSelectPosition;

	public PlayerLocalAdapter(Context context, String type,
			ArrayList<LocalVideoEpisode> localPlayLists, String videoname) {
		mContext = context;
		mType = type;
		mLocalPlayLists = localPlayLists;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mVideoName = videoname;
	}

	@Override
	public int getCount() {
		if (null != mLocalPlayLists) {
			return mLocalPlayLists.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (null != mLocalPlayLists) {
			return mLocalPlayLists.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	class VideoHolder {
		TextView tv;
		ImageView playTagIv;
		TextView episodePorderTv;
		TextView episodePorderNameTv;
		View gridItemLayout;
		View listItemLayout;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		VideoHolder holder = null;
		if (convertView == null) {
			holder = new VideoHolder();
			convertView = mInflater.inflate(
					R.layout.episode_player_local_item, null);
			holder.gridItemLayout = convertView.findViewById(R.id.grid_item_layout);
			holder.tv = (TextView) convertView.findViewById(R.id.episode_item_num);

			holder.listItemLayout = convertView.findViewById(R.id.list_item_layout);
			holder.playTagIv = (ImageView) convertView.findViewById(R.id.play_tag_iv);
			holder.episodePorderTv = (TextView) convertView.findViewById(R.id.episode_porder_tv);
			holder.episodePorderNameTv = (TextView) convertView.findViewById(R.id.episode_porder_name_tv);

			convertView.setTag(holder);
		} else {
			holder = (VideoHolder) convertView.getTag();
		}

		if (null != mLocalPlayLists) {
			LocalVideoEpisode episode = mLocalPlayLists.get(position);
			if(MoviesConstant.VT_TV.equals(mType)
					|| MoviesConstant.VT_CARTOON.equals(mType)){
				holder.gridItemLayout.setVisibility(View.VISIBLE);
				holder.listItemLayout.setVisibility(View.GONE);
				holder.tv.setText(episode.getPorder());
				holder.tv.setVisibility(View.VISIBLE);
				setGridSelectTextColor(holder, position);
			}else{
				holder.gridItemLayout.setVisibility(View.GONE);
				holder.listItemLayout.setVisibility(View.VISIBLE);
				holder.episodePorderTv.setText(episode.getPorder());
				holder.episodePorderNameTv.setText(episode.getName());
				setListSelectTextColor(holder, position);
			}
		}
		return convertView;
	}
	
	private String getReplaceName(LocalVideoEpisode episode, String name) {
		try {
			name = name.replaceFirst(mVideoName, "");
			name = name.replaceFirst(episode.getPorder(), "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return name.trim();
	}
	
	private void setGridSelectTextColor(VideoHolder holder, int position) {
		if (mSelectPosition == position) {
			holder.tv.setSelected(true);
			holder.tv.setVisibility(View.INVISIBLE);
			holder.playTagIv.setVisibility(View.VISIBLE);
		} else {
			holder.tv.setSelected(false);
			holder.tv.setVisibility(View.VISIBLE);
			holder.playTagIv.setVisibility(View.INVISIBLE);
		}
	}

	private void setListSelectTextColor(VideoHolder holder, int position) {
		if (mSelectPosition == position) {
			holder.episodePorderTv.setSelected(true);
			holder.episodePorderNameTv.setSelected(true);
			holder.playTagIv.setVisibility(View.VISIBLE);
		} else {
			holder.episodePorderTv.setSelected(false);
			holder.episodePorderNameTv.setSelected(false);
			holder.playTagIv.setVisibility(View.INVISIBLE);
		}
	}

	public void setmSelectPosition(int mSelectPosition) {
		this.mSelectPosition = mSelectPosition;
	}

}
